package com.sl.au.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.history.HistoricVariableInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.AuditNode;
import com.sl.au.entity.MyTask;
import com.sl.au.entity.User;
import com.sl.au.repository.AuditNodeRepository;
import com.sl.au.service.ActivitiService;
import com.sl.au.service.AuditNodeService;
import com.sl.au.service.UserService;

@Service("auditNodeService")
@Transactional
public class AuditNodeServiceImpl extends BaseServiceImpl implements AuditNodeService {

	@Autowired
	AuditNodeRepository auditNodeReposiotry;
	@Autowired
	ActivitiService activitiService;
	@Autowired
	UserService userService;

	@Override
	public AuditNodeRepository getRepository() {
		return this.auditNodeReposiotry;
	}

	@Override
	public List<AuditNode> getAll(String pInstId, String entityId) {
		List<AuditNode> nodes = new ArrayList<>();
		AuditNode node = null;
		List<HistoricTaskInstance> historyTasks = activitiService.findPorcessedTasks(pInstId);
		List<Task> tasks = null;

		if (null != historyTasks && historyTasks.size() > 0) {
			// 说明已经进行了，或可能已经完成了
			nodes = this.getRepository().getNodesById(pInstId, entityId);
		} // end of history task
			// get task in executing
		tasks = activitiService.getProcessEngine().getTaskService().createTaskQuery().processInstanceId(pInstId).list();
		String taskId = null;
		if (null != tasks && tasks.size() > 0) {
			// 说明审批流程还没有结束，正在进行中
			taskId = tasks.get(0).getId();
			if (null != taskId) {
				// 审批
				Map<String, Object> variables = activitiService.getVariablesByTaskId(taskId);
				int auditorNumber = Integer.parseInt(variables.get("auditorNumber").toString());

				// int unprocessedNumber=auditorNumber-nodes.size();
				String auditorId = null;
				User user = null;
				for (int i = nodes.size() + 1; i <= auditorNumber; i++) {
					auditorId = variables.get("auditor" + i).toString();
					if (StringUtils.hasText(auditorId)) {
						user = userService.getRepository().getOne(auditorId);
						auditorId = null;
						if (null != user) {
							node = new AuditNode();
							node.setAuditor(user);
							node.setCreateTime(null);
							nodes.add(node);
						}
					}
				} // end of for
			}
		}
		return nodes;
	}

	@Override
	public List<AuditNode> getHistoryActivity(String pInstId, String entityId) {
		List<AuditNode> nodes = new ArrayList<>();
		AuditNode node = null;	
		List<HistoricVariableInstance> list = activitiService.getProcessEngine().getHistoryService().createHistoricVariableInstanceQuery()
				.processInstanceId(pInstId).list();
		if (list != null && list.size() > 0) {
			for (HistoricVariableInstance hvi : list) {
				System.out.print("piId:" + hvi.getProcessInstanceId() + "，");
				System.out.print("variablesName:" + hvi.getVariableName() + "，");
				System.out.println("variablesValue:" + hvi.getValue() + ";");
			}//for			
		}//if
		return nodes;
	}
}
